from celery import shared_task
from .models import Stock
from .utils.stock_data import StockDataService

@shared_task
def update_stock_prices():
    """更新股票最新价格"""
    data_service = StockDataService()
    stocks = Stock.objects.all()
    
    for stock in stocks:
        try:
            # 获取实时价格
            price = data_service.get_real_time_price(stock.code)
            if price is not None:
                stock.last_price = price
                stock.save()
        except Exception as e:
            print(f"更新股票 {stock.code} 价格时出错: {str(e)}")

@shared_task
def send_daily_trading_suggestions():
    """每日发送交易建议到微信"""
    from .models import Stock
    from .utils.notification import NotificationService
    from .utils import TradingStrategy

    try:
        stocks = Stock.objects.all()
        suggestions = []
        
        for stock in stocks:
            strategy = TradingStrategy(stock)
            suggestion = strategy.get_trading_suggestion()
            if suggestion['suggestion'] != '观望':
                suggestions.append(suggestion)

        if suggestions:
            notification = NotificationService()
            notification.send_wechat(suggestions)

    except Exception as e:
        print(f"发送每日交易建议时出错: {str(e)}") 